package com.ruoyi.common.utils.mybatisPlus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import java.util.List;
import java.util.Objects;

@Data
public class SearchModel<T> {

    private Integer pageIndex;
    private Integer pageSize;
    private List<Field> fields;
    private String orderField;
    private boolean isAsc;

    public IPage<T> getPage() {
        IPage<T> page = new Page<>(pageIndex, pageSize);
        if (StringUtils.isNotEmpty(orderField)) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(isAsc);
            orderItem.setColumn(orderField);
            page.orders().add(orderItem);
        }
        return page;

    }

    public QueryWrapper<T> getQueryModel() {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(orderField)) {
            queryWrapper.orderBy(true, isAsc, orderField);
        }

        if (Objects.isNull(this.fields)) {
            return queryWrapper;
        }

        for (Field field : this.fields) {
            if(Objects.isNull(field.getQueryMethod())){
                queryWrapper.eq(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.eq)){
                queryWrapper.eq(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.in)){
                queryWrapper.in(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.like)){
                queryWrapper.like(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.gt)){
                queryWrapper.gt(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.lt)){
                queryWrapper.lt(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.ge)){
                queryWrapper.ge(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.le)){
                queryWrapper.le(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.between)){
                queryWrapper.between(true, field.getName(), field.getValue(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.ne)){
                queryWrapper.ne(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.notEq)){
                queryWrapper.ne(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.notIn)){
                queryWrapper.notIn(true, field.getName(), field.getValue());
            }else if(field.getQueryMethod().equals(QueryMethod.isNull)){
                queryWrapper.isNull(true, field.getName());
            }else if(field.getQueryMethod().equals(QueryMethod.isNotNull)){
                queryWrapper.isNotNull(true, field.getName());
            }
        }
        return queryWrapper;
    }
}
